package com.shrct.businessprocess.customerservice.impl;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.shrct.businessprocess.customerservice.RCRoutineService;
import com.shrct.businessprocess.customerservice.RCRoutineServiceItem;
import com.shrct.businessprocess.customerservice.RCRoutineServiceItemStatus;
import com.shrct.businessprocess.customerservice.RCRoutineServicePlan;
import com.shrct.businessprocess.customerservice.RCRoutineServicePlanItem;
import com.shrct.businessprocess.customerservice.RCRoutineServicePlanStatus;
import com.shrct.businessprocess.customerservice.RCRoutineServiceWorkItem;
import com.shrct.businessprocess.customerservice.impl.RCRoutineServicePlanItemImpl;
import com.shrct.businessprocess.customerservice.impl.RCRoutineServiceWorkItemImpl;
import com.shrct.businessprocess.customerservice.proxy.RCRoutineServicePlanProxy;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.organization.impl.RCEnterpriseImpl;
import com.shrct.businessprocess.product.RCLogicProduct;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IObjectFactory;
import edu.fudan.mylang.utils.DateUtil;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCROUTINESERVICEPLAN_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcroutineserviceplan")
public class RCRoutineServicePlanImpl extends BaseModelObject implements RCRoutineServicePlan {
  public RCRoutineServicePlanImpl() {
    super();
  }
  
  private String location;
  
  public String getLocation() {
    return this.location;
  }
  
  public RCRoutineServicePlan setLocation(final String location) {
    this.location = location;
    return this;			
    
  }
  
  @JoinColumn(name = "customer_id")
  @ManyToOne(targetEntity = RCEnterpriseImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEnterprise customer;
  
  public RCEnterprise getCustomer() {
    return this.customer;
  }
  
  public RCRoutineServicePlan setCustomer(final RCEnterprise customer) {
    this.customer = customer;
    return this;			
    
  }
  
  private Date planDate;
  
  public Date getPlanDate() {
    return this.planDate;
  }
  
  public RCRoutineServicePlan setPlanDate(final Date planDate) {
    this.planDate = planDate;
    return this;			
    
  }
  
  private RCRoutineServicePlanStatus status = RCRoutineServicePlanStatus.scheduled;
  
  public RCRoutineServicePlanStatus getStatus() {
    return this.status;
  }
  
  public RCRoutineServicePlan setStatus(final RCRoutineServicePlanStatus status) {
    this.status = status;
    return this;			
    
  }
  
  @OneToMany(targetEntity = RCRoutineServicePlanItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "plan")
  private Collection<RCRoutineServicePlanItem> planItems = new java.util.ArrayList<com.shrct.businessprocess.customerservice.RCRoutineServicePlanItem>();;
  
  public Collection<RCRoutineServicePlanItem> getPlanItems() {
    return this.planItems;
  }
  
  public RCRoutineServicePlanItem createAndAddtoPlanItems() {
    getPlanItems().size();
    com.shrct.businessprocess.customerservice.RCRoutineServicePlanItem rCRoutineServicePlanItem = new com.shrct.businessprocess.customerservice.impl.RCRoutineServicePlanItemImpl(this);
    getObjectFactory().create(rCRoutineServicePlanItem);
    getPlanItems().add(rCRoutineServicePlanItem);
    return rCRoutineServicePlanItem;
    
  }
  
  public RCRoutineServicePlanItem createAndAddtoPlanItems(final Procedure1<RCRoutineServicePlanItem> updater) {
    getPlanItems().size();
    com.shrct.businessprocess.customerservice.RCRoutineServicePlanItem rCRoutineServicePlanItem = new com.shrct.businessprocess.customerservice.impl.RCRoutineServicePlanItemImpl(this);
    getObjectFactory().create(rCRoutineServicePlanItem);
    getPlanItems().add(rCRoutineServicePlanItem);
    return rCRoutineServicePlanItem;
    
  }
  
  public RCRoutineServicePlan removeAllPlanItems() {
    for(RCRoutineServicePlanItem obj : this.planItems){
    	getObjectFactory().delete(obj);
    }
    this.planItems.clear();
    return this;	
    
  }
  
  public RCRoutineServicePlan removeFromPlanItems(final RCRoutineServicePlanItem rCRoutineServicePlanItem) {
    this.planItems.remove(rCRoutineServicePlanItem);
    getObjectFactory().delete(rCRoutineServicePlanItem);
    return this;
    
  }
  
  @JoinColumn(name = "workItem_id")
  @OneToOne(targetEntity = RCRoutineServiceWorkItemImpl.class, cascade = CascadeType.ALL)
  private RCRoutineServiceWorkItem workItem;
  
  public RCRoutineServiceWorkItem getWorkItem() {
    return this.workItem;
  }
  
  public RCRoutineServicePlan setWorkItem(final RCRoutineServiceWorkItem workItem) {
    this.workItem = workItem;
    return this;			
    
  }
  
  public Iterable<RCLogicProduct> getRelatedProducts() {
    final Function1<RCRoutineServicePlanItem,Collection<RCLogicProduct>> _function = new Function1<RCRoutineServicePlanItem,Collection<RCLogicProduct>>() {
        public Collection<RCLogicProduct> apply(final RCRoutineServicePlanItem it) {
          RCRoutineServiceItem _remender = it.getRemender();
          RCRoutineService _rountineService = _remender.getRountineService();
          Collection<RCLogicProduct> _products = _rountineService.getProducts();
          return _products;
        }
      };
    Iterable<Collection<RCLogicProduct>> _map = IterableExtensions.<RCRoutineServicePlanItem, Collection<RCLogicProduct>>map(this.planItems, _function);
    Iterable<RCLogicProduct> _flatten = Iterables.<RCLogicProduct>concat(_map);
    return _flatten;
  }
  
  public RCRoutineServiceWorkItem assign(final Procedure1<? super RCRoutineServiceWorkItem> preInit) {RCRoutineServiceWorkItem rCRoutineServiceWorkItem=RCRoutineServiceWorkItemImpl.create(getObjectFactory());
    final Procedure1<RCRoutineServiceWorkItem> _function = new Procedure1<RCRoutineServiceWorkItem>() {
        public void apply(final RCRoutineServiceWorkItem it) {
          preInit.apply(it);
          it.setPlan(RCRoutineServicePlanImpl.this);
          Date _day = DateUtil.today();
          it.setAssignedDate(_day);
          RCRoutineServicePlanImpl.this.status = RCRoutineServicePlanStatus.assigned;
          final Procedure1<RCRoutineServicePlanItem> _function = new Procedure1<RCRoutineServicePlanItem>() {
              public void apply(final RCRoutineServicePlanItem it) {
                RCRoutineServiceItem _remender = it.getRemender();
                _remender.assigned();
              }
            };
          IterableExtensions.<RCRoutineServicePlanItem>forEach(RCRoutineServicePlanImpl.this.planItems, _function);
        }
      };
    RCRoutineServiceWorkItem _doubleArrow = ObjectExtensions.<RCRoutineServiceWorkItem>operator_doubleArrow(rCRoutineServiceWorkItem, _function);
    RCRoutineServiceWorkItem _workItem = this.workItem = _doubleArrow;
    return _workItem;
  }
  
  public void addPlanItem(final RCRoutineServiceItem remender) {
    RCRoutineServicePlanItem _createAndAddtoPlanItems = this.createAndAddtoPlanItems();
    final Procedure1<RCRoutineServicePlanItem> _function = new Procedure1<RCRoutineServicePlanItem>() {
        public void apply(final RCRoutineServicePlanItem it) {
          it.setRemender(remender);
          RCRoutineServiceItem _remender = it.getRemender();
          _remender.setStatus(RCRoutineServiceItemStatus.scheduled);
        }
      };
    ObjectExtensions.<RCRoutineServicePlanItem>operator_doubleArrow(_createAndAddtoPlanItems, _function);
  }
  
  public void removePlanItem(final RCRoutineServiceItem remender) {
    final Function1<RCRoutineServicePlanItem,Boolean> _function = new Function1<RCRoutineServicePlanItem,Boolean>() {
        public Boolean apply(final RCRoutineServicePlanItem it) {
          RCRoutineServiceItem _remender = it.getRemender();
          boolean _equals = Objects.equal(_remender, remender);
          return Boolean.valueOf(_equals);
        }
      };
    final RCRoutineServicePlanItem toRemove = IterableExtensions.<RCRoutineServicePlanItem>findFirst(this.planItems, _function);
    this.removeFromPlanItems(toRemove);
  }
  
  public void finished() {
    this.status = RCRoutineServicePlanStatus.finished;
    final Procedure1<RCRoutineServicePlanItem> _function = new Procedure1<RCRoutineServicePlanItem>() {
        public void apply(final RCRoutineServicePlanItem it) {
          RCRoutineServiceItem _remender = it.getRemender();
          _remender.finished();
        }
      };
    IterableExtensions.<RCRoutineServicePlanItem>forEach(this.planItems, _function);
  }
  
  public void cancelWorkItem() {getObjectFactory().delete(this.workItem);
    
    this.workItem = null;
  }
  
  public static RCRoutineServicePlan create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.customerservice.RCRoutineServicePlan rCRoutineServicePlan = new com.shrct.businessprocess.customerservice.impl.RCRoutineServicePlanImpl(
    );
    objectFactory.create(rCRoutineServicePlan);
    return rCRoutineServicePlan;			
    
  }
  
  public static RCRoutineServicePlan createRCRoutineServicePlanImpl(final IObjectFactory objectFactory, final Procedure1<RCRoutineServicePlan> init) {
    com.shrct.businessprocess.customerservice.RCRoutineServicePlan rCRoutineServicePlan = new com.shrct.businessprocess.customerservice.impl.RCRoutineServicePlanImpl(
    );
    objectFactory.create(rCRoutineServicePlan);
    init.apply(rCRoutineServicePlan);
    
    return rCRoutineServicePlan;			
    
  }
  
  public RCRoutineServicePlanProxy toProxy() {
    com.shrct.businessprocess.customerservice.proxy.RCRoutineServicePlanProxy proxy = new com.shrct.businessprocess.customerservice.proxy.RCRoutineServicePlanProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
